var vm = new Vue({
    //vue对象节点,指向父盒子
    el:'#user',                                                                   
    //vue实例 的data属性
    data:function(){
        return{
          pageA:true,
          pageB:false,
          pageC:false,
          tname:'',
          pname:'',
          newFlag:true,
          createDate:[],
	      tableData:[],
	      multipleSelection:[],
	      //分页条
	      pageSize:10,
	      currPage:1,
	      total:1,
	      //
	      topicEditorEdit:null,
	      topic:{},
          statusList:[{value: '0', label: '正常'}],
          plateList:[],
          topicRules:{ 
        	  tname: [{ required: true, message: '请输入帖子主题', trigger: 'blur' },
			   	   { max: 20, message: '长度不能超过20个字符', trigger: 'blur' }],
			  pname: [{ required: true, message: '请选择所属板块', trigger: 'blur,change' }],
			  //tcontent: [{ required: true, message: '请输入主题内容', trigger: 'blur' }]
	      },
	      //
	      topicV:false,
	      topicData:{
	    	  "user":{},
	    	  "plate":{}
	      }
        };
    },
    //vue实例 的methods
    methods:{
       handleSizeChange:function(val) {
          vm.pageSize = val;
          vm.query();
       },
       handleCurrentChange:function(val) {
    	   vm.currPage = val;
    	   vm.query();
       },
       handleSelectionChange(val) {
           vm.multipleSelection = val;
       },
       //格式化日期
       dateFormat:function(row,column){
    	   var date = row[column.property];
    	   if(date == undefined){
    		   return "";
    	   }
    	   return moment(date).format("YYYY-MM-DD HH:mm:ss");
       },
       loadData:function(){
    	   
    	   vm.topicEditorEdit = UM.getEditor("topicEditorEdit");
    	   $('.edui-container').css("width","");
    	   $('.edui-container').css("height","600px");
    	   $('.edui-body-container').css("width","");
    	   
    	   var url = "./listTopic.action";
    	   if(vm.createDate == null || vm.createDate.length ==0 
    			   || typeof(vm.createDate[0]) == 'string' || typeof(vm.createDate[1]) == 'string'){
    		   vm.createDate = ['',''];
		   }else{
			   vm.createDate.splice(0,2,moment(vm.createDate[0]).format('YYYY-MM-DD'),
			   moment(vm.createDate[1]).add(1, 'days').format('YYYY-MM-DD'));
		   };
    	   var params = {
    		   'pagemap.size':vm.pageSize,
    		   'pagemap.page':vm.currPage,
    		   'pagemap.pname':vm.pname,
    		   'pagemap.tname':vm.tname,
    		   'pagemap.mindate':vm.createDate[0],
    		   'pagemap.maxdate':vm.createDate[1]
    	   }
    	   
    	   pb.ajaxPost(url,params,function(r){
	       		if(r.code == '000000'){
	       			/*if(r.plateList !== null &&r.topicList.length>0){
	       				for(var i=0;i<r.topicList.length;i++){
	       					var ai = r.topicList[i];
	       					vm.tableData.push(ai[0]);
	       				}
	       			}*/
	       			var nowDate = new Date().getTime();
	       			
	       			vm.tableData = r.topicList;
	       			if(vm.tableData !== null && vm.tableData.length>0){
	       				for(var i=0;i<vm.tableData.length;i++){
	       					var tempDate = new Date(vm.tableData[i].t_create_time).getTime();
	       					vm.tableData[i].flag = nowDate - tempDate > (1 * 3600 * 1000 * 24)?0:1;
	       					
	       					vm.topicEditorEdit.setContent(vm.tableData[i].tcontent);
	       					vm.tableData[i].tcontent = vm.topicEditorEdit.getContentTxt();
		       			}
	       			}
	       			vm.total = r.topiccount;
	       		}
	       },function(err){
	
	       });
       },
       getRecord(tid){
    	   var url = "./getTopicById.action";
    	   var params = {
    		   'topic.tid':tid
    	   }
    	   pb.ajaxPost(url,params,function(r){
	       		if(r.code == '000000'){
	       			vm.topicData = r.topic;
	       			vm.topicEditorEdit.setContent(vm.topicData.tcontent);
	       			vm.topicData.createDate = moment(vm.topicData.t_create_time).format('YYYY-MM-DD');
	       			vm.topicData.tcontent = vm.topicEditorEdit.getContent();
	       			$('#content').append(vm.topicData.tcontent);
	       			vm.topic = r.topic;
	       			vm.topic.pname = r.topic.plate.pname;
	       			
	       		}else{
	       		  pb.alertErr('获取信息失败');
	       		}
	       },function(err){
	    	   pb.alertErr('获取信息失败');
	       });
       },
       open(tid){
    	  /* $('#content').empty();
    	   vm.topicV = true;
    	   vm.getRecord(tid);*/
    	   window.parent.vm.goTo('viewTopic',tid);
       },
       query:function(){
    	   vm.loadData();
       },
       //新增
       add:function(){
    	   vm.topicEditorEdit.setContent('');
    	   vm.pageA=false;
    	   vm.pageB=true;
    	   vm.pageC=false;
    	   
       },
       //更新
       update:function(){
    	   if(vm.multipleSelection == null || vm.multipleSelection.length !=1){
    		   pb.alertWar("请选择一条记录");
    		   return false;
    	   }
    	   vm.pageA=false;
    	   vm.pageB=true;
    	   vm.pageC=false;
    	   vm.getRecord(vm.multipleSelection[0].tid);
       },
       //删除
       del:function(){
    	   if(vm.multipleSelection == null || vm.multipleSelection.length <=0){
    		   pb.alertWar("请选择至少一条记录");
    		   return false;
    	   }
    	   var tids = [];
    	   for(var i=0;i<vm.multipleSelection.length;i++){
    		   tids.push(vm.multipleSelection[i].tid);
    	   }
    	   
    	   vm.$confirm('确定删除么?','提示',{
               confirmButtonText: '确定',
               cancelButtonText: '取消',
               type:'warning',
               callback:function(e){
                   if(e === 'confirm'){
                	   var params = {
                		   "res.ids":JSON.stringify(tids)                		 
                	   }
                       pb.ajaxPost("./delTopic.action",params,function(res){
                           if(res.code == '000000'){
                        	   pb.alertSuc('删除成功');
                               vm.query();
                           }else{
                           	pb.alertErr('删除失败');
                           }
                       });
                   	
                   }
               }
           });
       },
       submit:function(formName){
    	   vm.$refs[formName].validate(function(valid) {
 	          if (valid) {
                  var url ="";
 	        	  if(vm.topic.tid == null || vm.topic.tid == ''){
 	        		 url = './addTopic.action';
 	        	  }else{
 	        		 url = './updateTopic.action';
 	        	  }
                  var params = {
                		"topic.tid":vm.topic.tid,
                  		"topic.tname":vm.topic.tname,
                  		"topic.plate.pid":vm.topic.pname,
                  		"topic.tcontent":vm.topicEditorEdit.getContent()
                  };
                  
                  pb.ajaxPost(url,params,function(r){
                  	if(r.code == '000000'){
                  		pb.alertSuc("提交成功");
                  		vm.back('topicRef');
                  	}else{
                  		pb.alertErr("提交失败");
                  	}
                  	},function(err){

                  		pb.alertErr("操作超时");
                  	});
 	          } else {
 	            return false;
 	          }
 	    });
       },
       back:function(formName){
    	   vm.pageA=true;
    	   vm.pageB=false;
    	   vm.pageC=false;
    	   vm.user = {};
    	   vm.$refs[formName].resetFields();
    	   vm.loadData(vm.pageSize,vm.currPage);
    	   vm.topicEditorEdit.setContent('');
       },
       getPlateList:function(){
    	   var url = "./listPlate.action";
    	   var params = {
    	   }
    	   pb.ajaxPost(url,params,function(r){
	       		if(r.code == '000000'){
	       			vm.plateList = r.plateList;
	       		}
	       });
       }


    }
    //vue实例 的methods结束
});
vm.loadData(10,1);
vm.getPlateList();